package com.datawin.taotaocheche.ui.actviity.user.presenter;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import com.datawin.taotaocheche.base.BaseBiz;
import com.datawin.taotaocheche.comm.MyApplication;
import com.datawin.taotaocheche.comm.MyType;
import com.datawin.taotaocheche.comm.RetrofitHelp;
import com.datawin.taotaocheche.event.CloseActEvent;
import com.datawin.taotaocheche.ui.actviity.login.LoginAct;
import com.datawin.taotaocheche.ui.actviity.user.biz.ForgetPwdBiz;
import com.datawin.taotaocheche.ui.actviity.user.contract.ForgetContract;
import com.datawin.taotaocheche.util.StringUtils;

/**
 * Created by Administrator on 2017/10/21.
 */

public class ForgetPwdPresenter implements ForgetContract.Presenter {

    ForgetContract.View view;
    ForgetPwdBiz forgetPwdBiz;


    @Inject
    public ForgetPwdPresenter(ForgetContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        forgetPwdBiz=(ForgetPwdBiz) biz;
    }

    @Override
    public void resetPwd(String oldPsw, String newPsw,String surePsw) {
        if(StringUtils.isEmpty(oldPsw)){
            view.showMsg("请输入旧密码");
            return;
        }
        if(StringUtils.isEmpty(newPsw)){
            view.showMsg("请输入新密码");
            return;
        }
        if(StringUtils.isEmpty(surePsw)){
            view.showMsg("请输入确认新密码");
            return;
        }
        if(!newPsw.equals(surePsw)){
            view.showMsg("两次输入新密码不一致");
            return;
        }
        view.loading(true);
        forgetPwdBiz.resetPwd(MyApplication.userInfo.getUserCode(), oldPsw, newPsw, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object o) {
                view.showMsg("修改密码成功");
                view.loading(false);
                MyApplication.userInfo=null;
                RetrofitHelp.logout();
                view.startAct(LoginAct.class,null);
                EventBus.getDefault().post(new CloseActEvent(MyType.Logout));
                view.finishAct();
            }

            @Override
            public void onFailure(Object o) {
                view.loading(false);
            }
        });
    }
}
